package cn.itcast.bos.web.action.base;




import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.base.StandardService;

@Controller
@Scope("prototype")
@Namespace(value="/")
@ParentPackage("struts-default")
public class StandardAction extends ActionSupport implements ModelDriven<Standard>{
	//模型驱动，将从表单获取的参数用model里面的对象进行封装
	Standard standard=new Standard();
	
	@Override
	public Standard getModel() {
		
		return standard;
	}
	
	@Autowired
	private StandardService standardService;
	
	/**
	 * 添加收派标准的方法
	 *
	 */

	@Action(value="standard_save",results={@Result(name="success",type="redirect",location="./pages/base/standard.html")})
	public String save(){
		//调用service层，进行数据处理
		standardService.save(standard);
		
		return SUCCESS;
	}

	
	
	/**
	 * 分页功能，将数据库的所有信息展现在页面上
	 * 
	 */
   //属性驱动将页面传递的参数接收，请求自带两个参数，一个是当前页page,一个是每页显示条数rows
	//定义这两个属性
	private int page;
	private int rows;
	//提供setter方法
	
	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}
	
	//定义分页的方法
	@Action(value = "standard_pageQuery"/*, results = {
			@Result(name = "success",type="dispatcher")}*/)
	public void pageQuery(){
	 
	Pageable pageable=new PageRequest(page-1,rows);//底层页数是从0开始计数，所以传递过来的参数得减一
	//调用service层，进行业务操作
	Page<Standard> pageData = standardService.findPageData(pageable);
	
	//返回地json数据需要total(所有的记录数，得到总分页数)和rows(当前页的数据),所以用map进行封装
	Map<String,Object> map=new HashMap<String,Object>();
	map.put("total", pageData.getTotalElements());
	map.put("rows", pageData.getContent());
	try {
	     ServletActionContext.getResponse().setCharacterEncoding("utf-8");
		ServletActionContext.getResponse().getWriter().write(JSON.toJSONString(map));
	
	} catch (IOException e) {
		
		e.printStackTrace();
	}
	
}
	
	
	/**
	 * 查询所有收派标准的方法
	 */
	@Action(value="standard_findAll")
	public void findAll(){
		
		
	List<Standard> list=standardService.findAll();
	//将数据返回页面	
		
	try {
		//解决中文乱码
	    ServletActionContext.getResponse().setCharacterEncoding("utf-8");
		ServletActionContext.getResponse().getWriter().write(JSON.toJSONString(list));
	
	} catch (IOException e) {
		
		e.printStackTrace();
	}
	}
}
